<template>
  <div>
    <a-modal
      title="填写意向单"
      v-model="visible"
      @ok="onSubmit"
      @cancel="onCancel"
    >
      <a-spin :spinning="loading">
        <a-form-model
          ref="addForm"
          :model="addForm"
          :rules="addRules"
          :label-col="{ span: '6' }"
          :wrapper-col="{ span: '18' }"
        >
          <a-form-model-item label="商标类型">
            <a-input type="text" disabled="" v-model="addForm.category"/>
          </a-form-model-item>
          <a-form-model-item label="类似群组">
            <a-input type="textarea" disabled="" v-model="addForm.groups"/>
          </a-form-model-item>
          <a-form-model-item label="受让方主体类型">
            <a-select v-model="addForm.applicant">
              <a-select-option v-for="(item, index) in addTypeOpts" :key="index" :value="index">{{item}}</a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="受让方姓名">
            <a-input type="text" v-model="addForm.applicant_name" />
          </a-form-model-item>
          <a-form-model-item label="受让方证件">
            <a-upload-dragger
              name="file"
              :multiple="true"
              :action="updURL"
              :headers="headers"
              @change="onChange"
            >
              <p class="ant-upload-drag-icon">
                <a-icon type="inbox" />
              </p>
              <p class="ant-upload-text">
                将文件拖拽至上传区域
              </p>
            </a-upload-dragger>
          </a-form-model-item>
          <a-form-model-item label="受让方联系电话">
            <a-input type="text" v-model="addForm.applicant_phone" />
          </a-form-model-item>
          <a-form-model-item label="客户预算">
            <a-input type="text" v-model="addForm.budget" />
          </a-form-model-item>
          <a-form-model-item label="备注">
            <a-textarea :rows="4" v-model="addForm.remake"></a-textarea>
          </a-form-model-item>
        </a-form-model>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
  import { getAddIntent, postAddIntent } from '@/api/transaction'
  import apis from '@/api'
  import storage from 'store'
  import { ACCESS_TOKEN } from '@/store/mutation-types'
  export default {
    props: {
      gid: {
        type: String,
        default: ''
      }
    },
    name: 'AddIntent',
    data () {
      return {
        addForm: {},
        addRules: {
          notes: [{ required: true, message: '要求修改，必须填写原因，方便专利师修改', trigger: 'blur' }]
        },
        loading: false,
        updURL: apis.Upload,
        headers: {
          'Access-Token': storage.get(ACCESS_TOKEN)
        },
        addTypeOpts: {},
        visible: true
      }
    },
    created () {
      this.getIntent(this.gid)
    },
    methods: {
      getIntent (id) {
        this.loading = true
        getAddIntent(id).then(res => {
          if (res.status === 0) {
            this.addForm.groups = res.result.groups
            this.addTypeOpts = res.result.appTypeOpts
            this.addForm.category = res.result.category
          }
        }).finally(() => {
          this.loading = false
        })
      },
      onCancel () {
        this.visible = false
        this.$emit('cancel', this.visible)
      },
      onSubmit () {
        const app = this
        const data = {}
        for (const k in app.addForm) {
          data['intent[' + k + ']'] = app.addForm[k]
        }
        app.$refs.addForm.validate(valid => {
          if (valid) {
            postAddIntent(app.gid, data).then(res => {
              if (res.status === 0) {
                app.onCancel()
              } else {
                app.$warning({
                  title: res.msg
                })
              }
            })
          }
        })
      },
      onChange (info) {
        const app = this
        if (info.file.status === 'uploading') {
          return false
        }
        if (info.file.status === 'done') {
          app.addForm.applicant_file = info.file.response.result.url
          app.addForm.applicant_filename = info.file.name
        }
      }
    }
  }
</script>

<style scoped>

</style>
